/*
 * ThermistorTable.hpp
 *
 * Copyright 2012 Pieter Agten
 *
 * This file is part of tltgen.
 *
 * tltgen is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * tltgen is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with tltgen.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @file   ThermistorTable.hpp
 * @author Pieter Agten (pieter.agten@gmail.com)
 * @date   30 sep 2012
 */

#ifndef THERMISTORTABLE_HPP
#define THERMISTORTABLE_HPP


#include <list>
#include <boost/lexical_cast.hpp>
#include <boost/signals2.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/version.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/shared_ptr.hpp>

#include "Temperature.hpp"
#include "Resistance.hpp"
#include "Voltage.hpp"
#include "AdcValue.hpp"
#include "Circuit.hpp"
#include "AnalogToDigitalConverter.hpp"
#include "ThermistorModel.hpp"
#include "ThermistorTableRow.hpp"

#include "SerializationException.hpp"


using boost::lexical_cast;
using boost::tuple;
using boost::shared_ptr;

namespace tltgen {

  class ThermistorTable {
  public:
    ThermistorTable();
    virtual ~ThermistorTable();

    enum Event {
      ROW_ADDED,
      ROW_CHANGED,
      ROW_REMOVED,
      BATCH_ENDED,
      TABLE_DELETE,
    };

    typedef boost::signals2::signal<void (Event, ThermistorTableRow*)> Signal;
    typedef Signal::slot_type Slot;

    bool isValidCircuit(const shared_ptr<Circuit const>& circuit) const;
    void setCircuit(const shared_ptr<Circuit const>& circuit);
    shared_ptr<Circuit const> getCircuit() const;
    bool hasCircuit() const;

    bool isValidAnalogToDigitalConverter(const shared_ptr<AnalogToDigitalConverter const>& adc) const;
    void setAnalogToDigitalConverter(const shared_ptr<AnalogToDigitalConverter const>& adc);
    shared_ptr<AnalogToDigitalConverter const> getAnalogToDigitalConverter() const;
    bool hasAnalogToDigitalConverter() const;

    bool hasColumn(ThermistorTableRow::Column col) const;
    std::string getColumnName(ThermistorTableRow::Column col) const;

    bool isValidValue(ThermistorTableRow::Column col, double v) const;
    bool isValidTemperature(Temperature t) const;
    bool isValidResistance(Resistance r) const;
    bool isValidVoltage(Voltage v) const;
    bool isValidAdcValue(AdcValue adcValue) const;

    void beginBatch();
    void endBatch();

    tuple<Temperature,Temperature> getTemperatureBounds() const;
    tuple<Resistance,Resistance> getResistanceBounds() const;
    tuple<Voltage,Voltage> getVoltageBounds() const;
    tuple<AdcValue,AdcValue> getAdcValueBounds() const;
    tuple<double,double> getBounds(ThermistorTableRow::Column col) const;

    ThermistorTableRow *createRow();
    void removeRow(ThermistorTableRow *row);
    unsigned int getNbRows() const;
    unsigned int getNbValidRows() const;
    std::list<ThermistorTableRow*>::const_iterator begin() const;
    std::list<ThermistorTableRow*>::const_iterator end() const;
    std::list<ThermistorTableRow*>::iterator begin();
    std::list<ThermistorTableRow*>::iterator end();

    boost::signals2::connection connectTableChangedObserver(const Slot &slot);

  protected:
    void observeRow(ThermistorTableRow *row);
    void observeAllRows();
    void onRowChange(ThermistorTableRow::Event e, ThermistorTableRow *row, ThermistorTableRow::Column col);
    void findBounds();
    void findBounds(ThermistorTableRow::Column col);

    shared_ptr<Circuit const> circuit;
    shared_ptr<AnalogToDigitalConverter const> adc;

    std::list<ThermistorTableRow*> rows;
    ThermistorTableRow* minRows[ThermistorTableRow::NB_COLUMNS];
    ThermistorTableRow* maxRows[ThermistorTableRow::NB_COLUMNS];

    int batchCount;
    Signal tableChanged;

  
    friend class boost::serialization::access;    
    template<class Archive>
    void save(Archive & ar, const unsigned int version) const
    {
      // version is always the latest when saving
      ar & this->circuit;
      ar & this->adc;
      ar & this->rows;
    }
    template<class Archive>
    void load(Archive & ar, const unsigned int version)
    {
      if (version != 0) {
	throw SerializationException("Unknown version " + 
				     lexical_cast<std::string>(version) + " for ThermistorTable.");
      }
      
      ar & this->circuit;
      ar & this->adc;
      ar & this->rows;
      this->observeAllRows();
      ////this->updateThermistorModels();
      this->findBounds();//TODO: auto find bounds when new row added/observed?
    }
    BOOST_SERIALIZATION_SPLIT_MEMBER()
    
  };
}


BOOST_CLASS_VERSION(tltgen::ThermistorTable, 0)
//BOOST_CLASS_EXPORT_KEY(tltgen::ThermistorTable)


#endif
